var express = require('express');
var router = express.Router();
var {navigationModel,sideModel}=require('../model/wrj')

router.post("/addNavigate",(req,res)=>{
  navigationModel.create(req.body)
  res.send({code:200})
})
router.get("/getNavigate",async(req,res)=>{
  let navigateList=await navigationModel.find()
  res.send({navigateList})
})

router.post("/addSide",(req,res)=>{
  sideModel.create(req.body)
  res.send({code:200})
})
router.get("/getSide",async(req,res)=>{
  let sideList=await sideModel.find().lean()
  let sideObj={}
  sideList.forEach(item=>{
    sideObj[item._id]=item
  })

  let list=[]
  sideList.forEach(item=>{
    if(!item["sid"]){
      list.push(item)
    }else{
      if(!sideObj[item["sid"]]["children"]){
        sideObj[item["sid"]]["children"]=[]
      }
      sideObj[item["sid"]]["children"].push(item)
    }
  })
  res.send({list,sideObj})
})

router.put('/editside',async(req,res)=>{
  await sideModel.updateMany(
    {sid:{$exists:true}},
    {$set:{level:2}}
  )
  res.send({code:200})
})

module.exports = router;
